package com.xmhmyh.generalframework.framework.aspectj;

import com.xmhmyh.generalframework.common.entity.Response;
import com.xmhmyh.generalframework.common.enums.ResponseCode;
import com.xmhmyh.generalframework.framework.exceptions.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;

/**
 * Title: 全局异常处理切面 Description: 利用 @ControllerAdvice + @ExceptionHandler
 * 组合处理Controller层RuntimeException异常
 */
@ControllerAdvice // 控制器增强
@ResponseBody
@Slf4j
public class ExceptionAspect {


    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Response handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error("could_not_read_json...", e);
        return Response.fail("could_not_read_json");
    }

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public Response handleValidationException(MethodArgumentNotValidException e) {
        log.error("parameter_validation_exception...", e);
        return Response.fail(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }


    /**
     * 405 - Method Not Allowed。HttpRequestMethodNotSupportedException
     * 是ServletException的子类,需要Servlet API支持
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Response handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        log.error("request_method_not_supported...", e);
        return Response.fail("request_method_not_supported");
    }

    /**
     * 415 - Unsupported Media Type。HttpMediaTypeNotSupportedException
     * 是ServletException的子类,需要Servlet API支持
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler({HttpMediaTypeNotSupportedException.class})
    public Response handleHttpMediaTypeNotSupportedException(Exception e) {
        log.error("content_type_not_supported...", e);
        return Response.fail("content_type_not_supported");
    }


    @ExceptionHandler(AccessDeniedException.class)
    public Response handlerException(AccessDeniedException e) {
        log.error("无权限访问", e);
        e.printStackTrace();
        return Response.fail(ResponseCode.NO_PERMISSION);
    }

    @ExceptionHandler({ManageExceptions.class})
    public Response handlerException(ManageExceptions e) {
        log.error("管理端错误", e.getMessage());
        e.printStackTrace();
        return Response.fail(e.getResponseCode());
    }

    @ExceptionHandler({WxExceptions.class})
    public Response handlerException(WxExceptions e) {
        log.error("微信端错误", e.getMessage());
        e.printStackTrace();
        return Response.fail(e.getResponseCode());
    }
    @ExceptionHandler({AppExceptions.class})
    public Response handlerException(AppExceptions e) {
        log.error("APP错误", e.getMessage());
        e.printStackTrace();
        return Response.fail(e.getResponseCode());
    }

    /**
     * Internal Server Error
     */
    @ExceptionHandler(Exception.class)
    public Response handleException(Exception e) {
        log.error("Internal Server Error...", e);
        return Response.fail("请求错误");
    }

    /**
     * Internal Server Error
     */
    @ExceptionHandler(H5Exceptions.class)
    public Response handleException(H5Exceptions e) {
        log.error("H5错误", e.getMessage());
        e.printStackTrace();
        return Response.fail(e.getResponseCode());
    }

    /**
     * Internal Server Error
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Response handleException(MissingServletRequestParameterException e) {
        return Response.fail(e.getMessage());
    }

}
